package cn.com.yto56.coresystem.business.msg.soa.biz.impl;

import cn.com.yto56.coresystem.business.msg.soa.biz.IMsgListBiz;
import cn.com.yto56.coresystem.common.msg.util.base.StringUtils;
import cn.com.yto56.coresystem.common.msg.util.db.IPage;
import cn.com.yto56.coresystem.common.msg.util.db.QueryPage;
import cn.com.yto56.coresystem.common.msg.util.domain.AppResult;
import cn.com.yto56.coresystem.logic.msg.soa.dao.IMsgListDao;
import cn.com.yto56.coresystem.logic.msg.soa.dao.IMsgTempletDao;
import cn.com.yto56.coresystem.logic.msg.soa.domain.MsgListBean;
import cn.com.yto56.coresystem.logic.msg.soa.domain.MsgTempletBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by wangrui on 2017/2/17.
 */
@Service
public class MsgListBizImpl implements IMsgListBiz {
    @Autowired
    private IMsgListDao msgListDaoImpl;
    @Autowired
    private IMsgTempletDao msgTempletDaoImpl;

    @Override
    public AppResult addMsgList(String sysInfo_Id, String userId, String msgInfo, String fixed_sendTime) {
        AppResult appResult = new AppResult();
        boolean resultFlag = false;
        String resultMessage = "";
        Map datas = null;
        if (StringUtils.isBlank(sysInfo_Id)) {
            resultMessage = "参数[yto_msg_sysInfo_Id]不能为空";
        } else if (StringUtils.isBlank(userId)) {
            resultMessage = "参数[yto_msg_userId]不能为空";
        } else if (StringUtils.isNotBlank(msgInfo)) {
            resultMessage = "参数[yto_msg_msgInfo]不能为空";
        } else {
            MsgListBean msgListBean = new MsgListBean();
            msgListBean.setYto_msg_sysinfo_id(sysInfo_Id);
            msgListBean.setYto_msg_userid(userId);
            msgListBean.setYto_msg_msginfo(msgInfo);
            msgListBean.setYto_msg_sendstate("0");
            msgListBean.setYto_msg_fixed_sendtime(fixed_sendTime);
            msgListBean.setCreate_time(new Date());
            int t = msgListDaoImpl.add(msgListBean);
            if (t <= 0) {
                resultMessage = "[系统异常]sql执行失败";
            } else {
                resultFlag = true;
                resultMessage = "执行成功";
            }
        }
        appResult.setResultFlag(resultFlag);
        appResult.setResultMessage(resultMessage);
        appResult.setDatas(datas);
        return appResult;
    }

    @Override
    public AppResult addMsgListForParam(String sysInfo_Id, String receiverId, String receiverType, String msgTempletId, Map msgTempletParam, String fixed_sendTime) {
        AppResult appResult = new AppResult();
        boolean resultFlag = false;
        String resultMessage = "";
        Map datas = null;
        if (StringUtils.isBlank(sysInfo_Id)) {
            resultMessage = "参数[yto_msg_sysInfo_Id]不能为空";
        } else if (StringUtils.isBlank(receiverId)) {
            resultMessage = "参数[yto_msg_receiverId]不能为空";
        } else if (StringUtils.isBlank(msgTempletId)) {
            resultMessage = "参数[yto_msg_msgTempletId]不能为空";
        } else if (msgTempletParam == null || msgTempletParam.isEmpty()) {
            resultMessage = "参数[yto_msg_msgTempletParam]不能为空";
        } else {
            MsgTempletBean msgTempletBean = new MsgTempletBean();
            msgTempletBean.setYto_msg_sysinfo_id(sysInfo_Id);
            msgTempletBean.setYto_msg_msgtempletid(msgTempletId);
            IPage<MsgTempletBean> msgTempletBeanIPage = msgTempletDaoImpl.search(msgTempletBean, new QueryPage(20, 1));
            List<MsgTempletBean> msgTempletBeanList = (List<MsgTempletBean>) msgTempletBeanIPage.getRows();
            if (msgTempletBeanList == null || msgTempletBeanList.size() <= 0) {
                resultMessage = "参数[yto_msg_msgTempletId]不存在或已被删除";
            } else {
                if (StringUtils.isBlank(receiverType)) {
                    receiverType = "0";
                }
                if (StringUtils.isBlank(fixed_sendTime)) {
                    fixed_sendTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
                }
                String templetInfo = msgTempletBeanList.get(0).getYto_msg_templetinfo();
                Set set = msgTempletParam.keySet();
                Iterator it = set.iterator();
                for (; it.hasNext(); ) {
                    String key = (String) it.next();
                    String value = (String) msgTempletParam.get(key);
                    templetInfo = templetInfo.replace("${" + key + "}", value);
                }

                if (receiverType.equals("0")) {
                    MsgListBean msgListBean = new MsgListBean();
                    msgListBean.setYto_msg_sysinfo_id(sysInfo_Id);
                    msgListBean.setYto_msg_msginfo(templetInfo);
                    msgListBean.setYto_msg_sendstate("0");
                    msgListBean.setYto_msg_fixed_sendtime(fixed_sendTime);
                    msgListBean.setCreate_time(new Date());
                    if (receiverType.equals("0")) {
                        msgListBean.setYto_msg_userid(receiverId);
                        int t = msgListDaoImpl.add(msgListBean);
                        if (t <= 0) {
                            resultMessage = "[系统异常]sql执行失败";
                        } else {
                            resultFlag = true;
                            resultMessage = "执行成功";
                        }
                    }
                } else if (receiverType.equals("1")) {
                    Map map = new HashMap();
                    map.put("yto_msg_sysInfo_Id", sysInfo_Id);
                    map.put("yto_msg_msgInfo", templetInfo);
                    map.put("yto_msg_userGroupId", receiverId);
                    map.put("yto_msg_fixed_sendTime", fixed_sendTime);
                    int t = msgListDaoImpl.addForGroup(map);
                    if (t <= 0) {
                        resultMessage = "sql执行失败或者该组没有用户";
                    } else {
                        resultFlag = true;
                        resultMessage = "执行成功";
                    }
                } else {
                    resultMessage = "请输入正确的yto_msg_sendstate";
                }


            }

        }
        appResult.setResultFlag(resultFlag);
        appResult.setResultMessage(resultMessage);
        appResult.setDatas(datas);
        return appResult;
    }

    @Override
    public AppResult getMsgList(String sysInfo_Id, String userId, String sendState, String fixed_sendTime, String sendTime) {
        AppResult appResult = new AppResult();
        boolean resultFlag = false;
        String resultMessage = "获取失败";
        List datas = null;
        if (StringUtils.isBlank(sysInfo_Id)) {
            resultMessage = "参数[yto_msg_sysInfo_Id]不能为空";
        } else if (StringUtils.isBlank(userId)) {
            resultMessage = "参数[yto_msg_userId]不能为空";
        } else {
            //TODO redis缓存
            MsgListBean msgListBean = new MsgListBean();
            msgListBean.setYto_msg_sysinfo_id(sysInfo_Id);
            msgListBean.setYto_msg_userid(userId);
            msgListBean.setYto_msg_fixed_sendtime(fixed_sendTime);
            msgListBean.setYto_msg_sendtime(sendTime);
            msgListBean.setYto_msg_sendstate(sendState);
            IPage<MsgListBean> msgListBeanIPage = msgListDaoImpl.search(msgListBean, new QueryPage(100, 1));
            List<MsgListBean> msgListBeanList = (List<MsgListBean>) msgListBeanIPage.getRows();
            List<Map> msgList = new ArrayList<Map>();
            if (msgListBeanList != null && msgListBeanList.size() > 0) {
                for (int i = 0; i < msgListBeanList.size(); i++) {
                    Map tempMap = new HashMap();
                    tempMap.put("yto_msg_msgId", String.valueOf(msgListBeanList.get(i).getId()));
                    tempMap.put("yto_msg_msgInfo", msgListBeanList.get(i).getYto_msg_msginfo());
                    msgList.add(tempMap);
                    msgListBeanList.get(i).setYto_msg_sendstate("1");
                }
                //当获取代发消息时
                if (StringUtils.isNotBlank(sendState) && sendState.equals("0")) {
                    //更新消息状态
                    updateMsgState(msgListBeanList);
                }

            }

            resultFlag = true;
            resultMessage = "获取成功";
            datas = msgList;
        }
        appResult.setResultFlag(resultFlag);
        appResult.setResultMessage(resultMessage);
        appResult.setDatas(datas);
        return appResult;
    }

    @Override
    public AppResult updateMsgListState(List<Map> list) {
        List<MsgListBean> msgListBeanList = new ArrayList<MsgListBean>();
        for (int i = 0; i < list.size(); i++) {
            MsgListBean msgListBean = new MsgListBean();
            msgListBean.setId((Integer) list.get(i).get("yto_msg_msgList_id"));
            msgListBean.setYto_msg_sendstate((String) list.get(i).get("yto_msg_sendState"));
            msgListBeanList.add(msgListBean);
        }
        return updateMsgListStateForBean(msgListBeanList);
    }


    public AppResult updateMsgListStateForBean(List<MsgListBean> msgListBeanList) {
        AppResult appResult = new AppResult();
        appResult.setResultFlag(false);
        appResult.setResultMessage("执行失败");
        int t = updateMsgState(msgListBeanList);
        if (t == msgListBeanList.size()) {
            appResult.setResultFlag(false);
            appResult.setResultMessage("执行成功");
        }
        return appResult;
    }


    @Transactional
    public int updateMsgState(List<MsgListBean> msgListBeanList) {
        int result = 0;
        if (msgListBeanList != null) {
            for (MsgListBean m : msgListBeanList) {
                Integer id = m.getId();
                String sendState = m.getYto_msg_sendstate();
                if (StringUtils.isBlank(sendState)) {
                    sendState = "1";
                }
                MsgListBean setMap = new MsgListBean();
                MsgListBean whereMap = new MsgListBean();
                if (sendState.equals("1")) {
                    setMap.setYto_msg_sendstate(sendState);
                    setMap.setYto_msg_sendtime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                    whereMap.setId(id);
                    whereMap.setYto_msg_sendstate("0");
                } else if (sendState.equals("2")) {
                    setMap.setYto_msg_sendstate(sendState);
                    setMap.setYto_msg_readtime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                    whereMap.setId(id);
                    whereMap.setYto_msg_sendstate("1");
                }
                int t = msgListDaoImpl.update(setMap, whereMap);
                if (t > 0) {
                    result++;
                }
            }
        }
        return result;
    }


}
